/* $Id: NewP4ProjectWizard.java,v 1.2 2009/05/13 16:57:35 satch Exp $ */
package org.jfunction.ui.internal.wizards;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard;
import org.jfunction.ui.internal.JFunctionUIPlugin;
import org.jfunction.ui.internal.ProjectNature;

/**
 * 
 * @author Doug Satchwell
 * @version $Id: NewP4ProjectWizard.java,v 1.2 2009/05/13 16:57:35 satch Exp $
 */
public class NewProjectWizard extends BasicNewResourceWizard implements INewWizard {
	private WizardNewProjectCreationPage wizardPage;

	@Override
	public void addPages() {
		super.addPages();
		this.wizardPage = new WizardNewProjectCreationPage("NewXSLProjectWizard");
		wizardPage.setTitle("P4 Project");
		wizardPage.setDescription("Create a new P4 project.");
		// wizardPage.setImageDescriptor(image);
		addPage(wizardPage);
	}

	public boolean performFinish() {
		WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
			@Override
			protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
				createProject(monitor == null ? new NullProgressMonitor() : monitor);
			}
		};
		try {
			getContainer().run(false, true, op);
		} catch (InvocationTargetException e) {
			JFunctionUIPlugin.log(e);
		} catch (InterruptedException e) {
			JFunctionUIPlugin.log(e);
		}
		return true;
	}

	protected void createProject(IProgressMonitor progressMonitor) throws CoreException {
		// create the project (getProject creates one!)
		IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(wizardPage.getProjectName());
		IProjectDescription newProjectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(newProject.getName());
		if (!Platform.getLocation().equals(wizardPage.getLocationPath()))
			newProjectDescription.setLocation(wizardPage.getLocationPath());

		String[] natureIds;
		ICommand[] builders;
		ICommand xslCommand = newProjectDescription.newCommand();
		xslCommand.setBuilderName(ProjectNature.BUILDER_ID);

		ICommand javaCommand = newProjectDescription.newCommand();
		javaCommand.setBuilderName(JavaCore.BUILDER_ID);

		{
			natureIds = new String[] { ProjectNature.NATURE_ID, JavaCore.NATURE_ID };
			builders = new ICommand[] { javaCommand, xslCommand };
		}

		newProjectDescription.setNatureIds(natureIds);
		newProjectDescription.setBuildSpec(builders);

		newProject.create(newProjectDescription, progressMonitor);
		progressMonitor.worked(20);

		newProject.open(progressMonitor);
		progressMonitor.worked(30);

		IFolder srcFolder = newProject.getFolder("src");
		srcFolder.create(true, true, new SubProgressMonitor(progressMonitor, 10));

		IJavaProject javaProject = JavaCore.create(newProject);

		progressMonitor.worked(20);

		List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
		entries.add(JavaCore.newSourceEntry(javaProject.getPath().append("src")));
		entries.add(JavaRuntime.getDefaultJREContainerEntry());
		javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), new SubProgressMonitor(progressMonitor, 20));

	}
}
